window.onload= function () {
    var url = '/shop/index',
        config = {
            elem: '#test'
            ,url:url
            ,cols: [[
                {field:'id', width:60, title: 'id', sort: true}
                ,{field:'shop_name', width:130, title: '商家名称'}
                ,{field:'service_rate', width:150, title: '提现服务费率(%)', sort: true}
                ,{field:'create_time', width:140, title: '入住时间',templet: function (item) {
                        return timestampToTime(parseInt(item.create_time));
                    }}
                ,{field:'shop_id', title: '绑定状态', width: 86,templet: function(d){
                        var bind = d.user_id;
                        if(bind){
                            return '<a href="/shop-user/info?id='+d.user_id+'" style="color:#009688;">已绑定</a>'
                        }else{
                            return '未绑定'
                        }
                    }}
                ,{field:'status', width:90, title: '状态',templet: function (item) {
                        switch (parseInt(item.status)){
                            case 1:
                                return '审核成功';
                            case 2:
                                return '驳回';
                            case 3:
                                return '停用';
                            default:
                                return '待审核'
                        }
                    }}
                ,{field:'admin_phone', title: '电话', width: 120}
                ,{fixed:'right', minWidth:300, title:'操作',align:'center', toolbar: '#barDemo'} //这里的toolbar值是模板元素的选择器

            ]]
            ,page: true
        };

    layui.use(['form','layer','jquery','table'], function(){
        var table = layui.table,
            form = layui.form,
            // layer = parent.layer === undefined ? layui.layer : top.layer,
            layer = layui.layer,
            $ = layui.jquery,
            operation = {
                "setServer":function (data) {
                    Utils.layer.closeAll();
                    Utils.layer.load(1,{'shade':0.5});
                    $.ajax({
                        url: '/shop/set-service-rate',
                        data:data,
                        method:'post',
                        dataType:'json',
                        success:function (data) {
                            Utils.layer.closeAll();
                            Utils.layer.msg(data.msg);
                            table.render(config);
                        }
                    });
                    return true;
                },
                "setStatus":function (data) {
                    $.ajax({
                        url: '/shop/modify-status',
                        data:data,
                        method:'post',
                        dataType:'json',
                        success:function (data) {
                            Utils.layer.closeAll();
                            Utils.layer.msg(data.msg);
                            table.render(config);
                        }
                    });
                    return true;
                },
                "setReject":function (data) {
                    $.ajax({
                        url: '/shop/modify-statustoregect',
                        data:data,
                        method:'post',
                        dataType:'json',
                        success:function (data) {
                            Utils.layer.closeAll();
                            Utils.layer.msg(data.msg);
                            table.render(config);
                        }
                    });
                    return true;
                }
            };
        var status = $('#status').val();
        config.url = '/shop/index?status='+status;
        table.render(config);
        //监听工具条
        table.on('tool(barDemo)', function(obj){ //注：tool是工具条事件名，test是table原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的DOM对象
            switch (layEvent){
                case 'detail':
                    if(data.user_id){
                        console.log(1)
                        location.href = '/shop/get-info?id='+data.id+'&type=1';                        
                    }else{
                        console.log(1)
                        location.href = '/shop/get-info?id='+data.id;
                    }
                    break;
                case 'edit':
                    location.href = '/shop/update-shop?id='+data.id;
                    break;
                case 'add-shop':
                    location.href = '/shop/add-shop';
                    break;
                case 'set-server':
                    var value = data.service_rate ? data.service_rate :'' ;
                    Utils.layer.open({
                        "content":[
                            '<form class="layui-form" action="">',
                            '<div class="layui-form-item">',
                            '<label class="layui-form-label">服务费率</label>',
                            '<div class="layui-input-block">',
                            '      <input type="text" id="service_rate" name="" lay-verify="number" placeholder="请输入服务费率" autocomplete="off" class="layui-input" value="'+ value+'">',
                            '</div></div></form>'
                        ].join(''),
                        "btn":['确认','取消'],
                        "yes":function () {
                            var number = $("#service_rate").val();

                            if(!(/^[0-9]+(.[0-9]{3})?$/.test(number))){
                                Utils.layer.msg('请输入正确的服务费率');
                                return false;
                            }
                            operation.setServer({
                                'id':data.id,
                                'value':number
                            });
                        },
                        "btn2":function () {
                            Utils.layer.closeAll();
                        }
                    });
                    break;
                case 'reject':
                    Utils.layer.open({
                        "content":[
                            '<form class="layui-form" action="">',
                            '<div class="layui-form-item">',
                            '<label class="layui-form-label">驳回原因</label>',
                            '<div class="layui-input-block">',
                            '      <textarea name="notify" placeholder="请输入内容" class="layui-textarea"></textarea>',
                            '</div></div></form>'
                        ].join(''),
                        "btn":['确认','取消'],
                        "yes":function () {
                            var notify = $('[name=notify]').val();

                            if(!notify) {
                                Utils.layer.msg('请输入驳回原因');
                                return false;
                            }
                            operation.setReject({
                                'id':data.id,
                                'dismissal':notify
                            });
                        },
                        "btn2":function () {
                            Utils.layer.closeAll();
                        }
                    });
                    break;
                default:
                    var status;
                    // 确认状态值.
                    switch (parseInt(data.status)) {
                        case 1:
                            status = 3;
                            break;
                        case 0:
                        case 3:
                            status = 1;
                            break;
                    }
                    // 只是普通的启用.
                    if(status) {
                        var text = status == 3 ? '停用此账号' : '启用此账号';
                        Utils.layer.confirm('您确认要' + text + '?',function () {
                            Utils.layer.closeAll()
                            operation.setStatus({
                                id: data.id,
                                status: status
                            })
                        });
                        return false;
                    }


            }
        });

        //搜索
        form.on("submit(serch-form)",function(data){
            var shop_name = ($("input[name='shop_name']").val()) ? $("input[name='shop_name']").val() : '';
            var status = ($('#status option:selected').val()) ? $('#status option:selected').val() : '';
            var url = '/shop/index?shop_name='+ shop_name +'&status='+ status;
            config.url = url;
            table.render(config);
            return false;
        });

    });
}